package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.ChainGoodsImagesService;
import com.junzhiit.nengji.cms.web.exception.GoodsImagesNotFound;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.ChainGoodsImagesAddVO;
import com.junzhiit.nengji.cms.web.vo.ChainGoodsImagesVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.ChainGoodsImagesDAO;
import com.junzhiit.nengji.model.ChainGoodsImages;

@Service
public class ChainGoodsImagesServiceImpl extends BaseServiceImpl implements ChainGoodsImagesService{
	
	@Autowired
	private ChainGoodsImagesDAO chainGoodsImagesDAO;

	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.chain.goods}")
	private String chainGoodsPath;
	
	@Value("${images.chain.goods.thumbnails.path}")
	private String imagesThumbnailsPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO findByChainGoodsId(String chainGoodsId) {
		ResultVO result = new ResultVO();

		List<ChainGoodsImages> chainGoodsImages = chainGoodsImagesDAO.findByChainGoodsId(chainGoodsId);
		if (chainGoodsImages == null || chainGoodsImages.isEmpty()) {
			return result;
		}

		Map<String, List<ChainGoodsImagesVO>> map = new HashMap<String, List<ChainGoodsImagesVO>>();
		for (ChainGoodsImages chainGoodsImage : chainGoodsImages) {

			ChainGoodsImagesVO chainGoodsImagesVO = new ChainGoodsImagesVO();
			BeanUtils.copyProperties(chainGoodsImage, chainGoodsImagesVO);
			String type = chainGoodsImage.getType().toString();

			if (map.containsKey(type)) {
				map.get(type).add(chainGoodsImagesVO);
			} else {
				List<ChainGoodsImagesVO> list = new ArrayList<ChainGoodsImagesVO>();
				list.add(chainGoodsImagesVO);
				map.put(type, list);
			}

		}
		result.setData(map);

		return result;
	}

	@Override
	public ResultVO add(List<ChainGoodsImagesAddVO> chainGoodsImagesAddVOs) {
		ResultVO result = new ResultVO();
		if (chainGoodsImagesAddVOs == null || chainGoodsImagesAddVOs.isEmpty()) {
			return result;
		}

		List<ChainGoodsImages> chainGoodsImagesList = new ArrayList<ChainGoodsImages>();
		for (ChainGoodsImagesAddVO chainGoodsImagesAddVO : chainGoodsImagesAddVOs) {
			ChainGoodsImages chainGoodsImages = new ChainGoodsImages();
			BeanUtils.copyProperties(chainGoodsImagesAddVO, chainGoodsImages);
			chainGoodsImagesList.add(chainGoodsImages);
		}

		int data = chainGoodsImagesDAO.batchInsert(chainGoodsImagesList);

		for (ChainGoodsImagesAddVO chainGoodsImagesAddVO : chainGoodsImagesAddVOs) {
			//FileUtil.moveFile(uploadPath + chainGoodsImagesAddVO.getFileName(), chainGoodsPath);
			//图片压缩
			//ThumbnailsUtil.toScale(chainGoodsPath+chainGoodsImagesAddVO.getFileName(), imagesThumbnailsPath, chainGoodsImagesAddVO.getFileName(), 1f, 0.5f);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,chainGoodsImagesAddVO.getFileName(), chainGoodsPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+chainGoodsImagesAddVO.getFileName());
			}
		}

		result.setData(data);

		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		ChainGoodsImages goodsImages = chainGoodsImagesDAO.query(id);
		if (goodsImages == null) {
			throw new GoodsImagesNotFound("图片不存在");
		}

		//FileUtil.removeFile(chainGoodsPath + goodsImages.getFileName());
		AliyunOSSClientUtil.removeFile(backetName, chainGoodsPath+goodsImages.getFileName());
		boolean data = chainGoodsImagesDAO.delete(id);
		result.setData(data);
		return result;
	}


}
